package com.py.androidUtils.UI.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebView {
	public WebView webView;
	private Context mContext;
	private String url;

	public MyWebView(Context context, WebView webView) {
		this.mContext = context;
		this.webView = webView;
		initView();
	}

	@SuppressLint({ "InflateParams", "SetJavaScriptEnabled" })
	public void initView() {
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setGeolocationEnabled(true);
		webView.getSettings().setDomStorageEnabled(true);
		GeoClient geo = new GeoClient();
		webView.setWebViewClient(new PTCWebViewClient(mContext, webView));
		webView.setWebChromeClient(geo);
		String origin = "";
		geo.onGeolocationPermissionsShowPrompt(origin, new Callback() {
			@Override
			public void invoke(String origin, boolean allow, boolean retain) {

			}
		});
	}

	public void loadUrl(String url) {
		webView.loadUrl(url);
	}

	public class PTCWebViewClient extends WebViewClient {

		public PTCWebViewClient(Context con, final WebView web) {

		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}

	}

	public class GeoClient extends WebChromeClient {
		@Override
		public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
			super.onGeolocationPermissionsShowPrompt(origin, callback);
			callback.invoke(origin, true, false);
		}
	}
}
